import{S as e,ut as t}from"./B603vO-13smUKN.js";import{Ht as n,gn as r,it as i,un as a}from"./CIxmKYxc4tZcJF.js";import{o}from"./BbxvatETxeTHf.js";import{i as s}from"./qOf9hsdh3aD96J.js";import"./C3Xax3Tz4BagVD.js";import{a as c,f as l,m as u,n as d,o as f,t as p}from"./mI_k9xko4swc5U.js";var m=t(`app:pdf-cache`);function h(){m(`usePdfCache`);let t={},h=r({}),g;async function _(){return g===void 0&&(g=await c()),g}let v=s({name:`thumbnailsCache`,maxParallel:20,idConflictResolution:`prioritize`});async function y(e,n,r=!1){m(`thumbnail: thumbnail`,e,v);let i=t[e];if(i)return m(`thumbnail: found in memcache`,e,i),i;let a=await _();if(a){let t=await a.getAsUrl(e);if(t)return m(`thumbnail: found in webcache`,e,t),t}if(r){let{promise:t}=v.enqueue(async()=>{m(`thumbnail: thumbnail generate task`,e);let t=await n();if(t)return m(`thumbnail: thumbnail with data`,t),await C(e,t);m.warn(`thumbnail did not get data`,e)});return await t}}async function b(e,t=!0){let n=l(e);if(n)return await y(n,async()=>{let t=await o.workspace?.assets.read(e.cid,e.idx);if(t)return await p(t)},t)}async function x(e,t,n=!0){return await y(e,async()=>t,n)}async function S(e,t){let n=await p(t);if(m(`loaded pdf source`,n,e,t),n)return C(e,n)}async function C(e,n){try{let r=await d(n);if(n.loadingTask.destroy(),r!=null){let n=await _();n&&await n.putBlob(e,r);let i=URL.createObjectURL(r);return t[e]=i,i}}catch(t){m.warn(`addDocument error`,e,t)}}function w(e){v.cancel(e)}function T(r){let o=e();function s(e){if(!e||h[e]!=null)return;let n=u(e);if(n==null)return;let r=l(n);v.enqueue(async n=>{let i=t[r];if(i){m(`thumbnail: found in memcache`,r,i),h[e]=i;return}let a=await _();if(a){let t=await a.getAsUrl(r);if(t){m(`thumbnail: found in webcache`,r,t),h[e]=t;return}}n.progress.isCancelled()||(m(`thumbnail:generate`),o.add(f(e,async t=>{if(m(`thumbnail: onAsset`,e),t?.bin){let n=await p(t?.bin);if(n){let t=await C(r,n);t&&(h[e]=t)}}})))},{id:r})}a(r)&&(m(`watch`,r),n(r,s,{immediate:!0}),i(o))}return{thumbnails:h,thumbnailForAsset:b,thumbnailForPdf:x,addDocumentBySource:S,cancel:w,useThumbnail:T}}var g=h();export{g as t};